package winOrDie.graphicsInterface.GameMenu;

import java.util.Vector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import winOrDie.Configuration.WoD_Configuration;


public class Composite_Configuration extends Composite {

	private Label label_Configuration = null;
	
	private Group group_COM_Ports = null;
	
	private Composite composite_Control_Port = null;
	private Label label_Control_Port = null;
	private Text text_Control_port = null;
	
	private Composite composite_RT_Port = null;
	private Label label_RT_Port = null;
	private Text text_RT_Port = null;
	
	private Composite composite_Voice_Port = null;
	private Label label_Voice_Port = null;
	private Text text_Voice_Port = null;
	
	private Group group_Servers = null;
	
	private Composite composite_Web_Server = null;
	private Label label_Web_Server = null;
	private Text text_Web_Server = null;
	private Label label_Web_Server_Port = null;
	private Text text_Web_Server_Port = null;
	
	private Composite composite_Proxy_Server = null;
	private Label label_Proxy_Server = null;
	private Text text_Proxy_Server = null;
	
	private Composite composite_Buttons = null;
	private Button button_Save = null;
	private Button button_Cancel = null;
	
	

	
	
	
	private Vector<Composite_Configuration_Listener> listeners = null;

	public synchronized void add_Composite_Configuration_Listener(Composite_Configuration_Listener listener)
	{
		listeners.add(listener);
	}

	public synchronized void remove_Composite_Configuration_Listener(Composite_Configuration_Listener listener)
	{
		listeners.remove(listener);
	}

	protected void fire_Composite_Configuration_Event(Composite_Configuration_Event event)
	{
		for (Composite_Configuration_Listener listener : listeners) {
			listener.on_Composite_Configuration_Change(event);
		}
	}
	
	
	private WoD_Configuration woDConfiguration = null;
	
	public WoD_Configuration getWoDConfiguration() {
		return woDConfiguration;
	}

	public void setWoDConfiguration(WoD_Configuration woDConfiguration) {
		this.woDConfiguration = woDConfiguration;
		show_Configuration();
	}

	public void show_Configuration_Connection_Error()
	{
		text_Control_port.setText(String.valueOf(woDConfiguration.getControl_Port()));
		text_RT_Port.setText(String.valueOf(woDConfiguration.getRT_Port()));
		text_Voice_Port.setText(String.valueOf(woDConfiguration.getVoice_Port()));
		text_Web_Server.setText("");
		text_Web_Server_Port.setText("");
		text_Proxy_Server.setText(woDConfiguration.getProxy_Server());
	}
	
	public void show_Configuration()
	{
		text_Control_port.setText(String.valueOf(woDConfiguration.getControl_Port()));
		text_RT_Port.setText(String.valueOf(woDConfiguration.getRT_Port()));
		text_Voice_Port.setText(String.valueOf(woDConfiguration.getVoice_Port()));
		text_Web_Server.setText(woDConfiguration.getWeb_Server());
		text_Web_Server_Port.setText(woDConfiguration.getWeb_Server_Port());
		text_Proxy_Server.setText(woDConfiguration.getProxy_Server());
	}
	
	private void fill_Configuration()
	{
		woDConfiguration.setControl_Port(Integer.valueOf(text_Control_port.getText()));
		woDConfiguration.setRT_Port(Integer.valueOf(text_RT_Port.getText()));
		woDConfiguration.setVoice_Port(Integer.valueOf(text_Voice_Port.getText()));
		woDConfiguration.setWeb_Server(text_Web_Server.getText());
		woDConfiguration.setWeb_Server_Port(text_Web_Server_Port.getText());
		woDConfiguration.setProxy_Server(text_Proxy_Server.getText());
	}
	
	public Composite_Configuration(Composite parent, int style) {
		super(parent, style);
		listeners = new Vector<Composite_Configuration_Listener>();
		woDConfiguration = new WoD_Configuration();
		initialize();
	}

	private void initialize() {
		setSize(new Point(300, 200));

		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		setLayout(gridLayout);
		
		get_Label_Configuration();
		
		/**
		 * Communications ports
		 */
		get_Group_COM_Ports();
		get_Text_Control_Port();
		get_Text_RT_Port();
		get_Text_Voice_Port();
		
		/**
		 * Servers
		 */
		get_Group_Servers();
		get_Text_Web_Server();
		get_Text_Web_Server_Port();
		get_Text_Voice_Port();
		get_Text_Proxy_Server();
		
		/**
		 * Buttons
		 */
		get_Composite_Buttons();
		
	}

	
	private Label get_Label_Configuration()
	{
		if (label_Configuration == null)
		{
			GridData gridData = new GridData();
			gridData.horizontalSpan = 1;
			
			label_Configuration = new Label(this, SWT.NONE);
			label_Configuration.setText("Configuración");
			
			label_Configuration.setLayoutData(gridData);
		}
		
		return label_Configuration;
	}
	
	private Group get_Group_COM_Ports()
	{
		if (group_COM_Ports == null)
		{
			GridLayout gridLayout = new GridLayout();
			gridLayout.numColumns = 1;
			
			GridData gridData = new GridData();
			gridData.horizontalSpan = 1;
			gridData.grabExcessHorizontalSpace = true;
			gridData.horizontalAlignment = SWT.FILL;
			
			group_COM_Ports = new Group(this, SWT.NONE);
			group_COM_Ports.setLayout(gridLayout);
			group_COM_Ports.setLayoutData(gridData);
			
			group_COM_Ports.setText("Puertos de comunicaciones");
		}
		
		return group_COM_Ports;
	}
	
	private Text get_Text_Control_Port()
	{
		if (text_Control_port == null)
		{
			GridLayout gridLayout = new GridLayout();
			gridLayout.numColumns = 2;
			
			GridData gridData_Composite_Control_Port = new GridData();
			gridData_Composite_Control_Port.grabExcessHorizontalSpace = true;
			gridData_Composite_Control_Port.horizontalAlignment = SWT.FILL;
			
			composite_Control_Port = new Composite(group_COM_Ports, SWT.NONE);
			composite_Control_Port.setLayout(gridLayout);
			composite_Control_Port.setLayoutData(gridData_Composite_Control_Port);
			
			label_Control_Port = new Label(composite_Control_Port, SWT.NONE);
			label_Control_Port.setText("Puerto de Control:");
			
			GridData gridData_Text_Control_Port = new GridData();
			gridData_Text_Control_Port.grabExcessHorizontalSpace = true;
			gridData_Text_Control_Port.horizontalAlignment = SWT.FILL;
			text_Control_port = new Text(composite_Control_Port, SWT.BORDER);
			text_Control_port.setLayoutData(gridData_Text_Control_Port);
			
		}
		return text_Control_port;
	}
	
	
	private Text get_Text_RT_Port()
	{
		if (text_RT_Port == null)
		{
			GridLayout gridLayout = new GridLayout();
			gridLayout.numColumns = 2;
			
			GridData gridData_Composite_RT_Port = new GridData();
			gridData_Composite_RT_Port.grabExcessHorizontalSpace = true;
			gridData_Composite_RT_Port.horizontalAlignment = SWT.FILL;
			
			composite_RT_Port = new Composite(group_COM_Ports, SWT.NONE);
			composite_RT_Port.setLayout(gridLayout);
			composite_RT_Port.setLayoutData(gridData_Composite_RT_Port);
			
			label_RT_Port = new Label(composite_RT_Port, SWT.NONE);
			label_RT_Port.setText("Puerto de juego:");
			
			GridData gridData_Text_RT_Port = new GridData();
			gridData_Text_RT_Port.grabExcessHorizontalSpace = true;
			gridData_Text_RT_Port.horizontalAlignment = SWT.FILL;
			
			text_RT_Port = new Text(composite_RT_Port, SWT.BORDER);
			text_RT_Port.setLayoutData(gridData_Text_RT_Port);
			
		}
		
		return text_RT_Port;
	}
	
	private Text get_Text_Voice_Port()
	{
		if (text_Voice_Port == null)
		{
			GridLayout gridLayout = new GridLayout();
			gridLayout.numColumns = 2;
			
			GridData gridData_Composite_Voice_Port = new GridData();
			gridData_Composite_Voice_Port.grabExcessHorizontalSpace = true;
			gridData_Composite_Voice_Port.horizontalAlignment = SWT.FILL;
			
			composite_Voice_Port = new Composite(group_COM_Ports, SWT.NONE);
			composite_Voice_Port.setLayout(gridLayout);
			composite_Voice_Port.setLayoutData(gridData_Composite_Voice_Port);
			
			label_Voice_Port = new Label(composite_Voice_Port, SWT.NONE);
			label_Voice_Port.setText("Puerto de Voz:");
			
			GridData gridData_Text_Voice_Port = new GridData();
			gridData_Text_Voice_Port.grabExcessHorizontalSpace = true;
			gridData_Text_Voice_Port.horizontalAlignment = SWT.FILL;
			text_Voice_Port = new Text(composite_Voice_Port, SWT.BORDER);
			text_Voice_Port.setLayoutData(gridData_Text_Voice_Port);
		}
		
		return text_Voice_Port;
	}
	
	
	private Group get_Group_Servers()
	{
		if (group_Servers == null)
		{
			GridLayout gridLayout = new GridLayout();
			gridLayout.numColumns = 1;
			
			GridData gridData = new GridData();
			gridData.horizontalSpan = 1;
			gridData.grabExcessHorizontalSpace = true;
			gridData.horizontalAlignment = SWT.FILL;
			
			group_Servers = new Group(this, SWT.NONE);
			group_Servers.setLayout(gridLayout);
			group_Servers.setLayoutData(gridData);
			
			group_Servers.setText("Servidores");
		}
		
		return group_COM_Ports;
	}
	
	public Text get_Text_Web_Server()
	{
		if (text_Web_Server == null)
		{
			GridLayout gridLayout = new GridLayout();
			gridLayout.numColumns = 2;
			
			GridData gridData_Composite_Web_Server = new GridData();
			gridData_Composite_Web_Server.grabExcessHorizontalSpace = true;
			gridData_Composite_Web_Server.horizontalAlignment = SWT.FILL;
			
			composite_Web_Server = new Composite(group_Servers, SWT.NONE);
			composite_Web_Server.setLayout(gridLayout);
			composite_Web_Server.setLayoutData(gridData_Composite_Web_Server);
			
			label_Web_Server = new Label(composite_Web_Server, SWT.NONE);
			label_Web_Server.setText("IP servidor Web:");
			
			GridData gridData_Text_Web_Server = new GridData();
			gridData_Text_Web_Server.grabExcessHorizontalSpace = true;
			gridData_Text_Web_Server.horizontalAlignment = SWT.FILL;
			text_Web_Server = new Text(composite_Web_Server, SWT.BORDER);
			text_Web_Server.setLayoutData(gridData_Text_Web_Server);
			
		}
		
		return text_Web_Server;
	}
	
	public Text get_Text_Web_Server_Port()
	{
		if(text_Web_Server_Port == null)
		{
			label_Web_Server_Port = new Label(composite_Web_Server, SWT.NONE);
			label_Web_Server_Port.setText("Puerto:");
			
			GridData gridData_Text_Web_Server_Port = new GridData();
			gridData_Text_Web_Server_Port.grabExcessHorizontalSpace = true;
			gridData_Text_Web_Server_Port.horizontalAlignment = SWT.FILL;
			text_Web_Server_Port = new Text(composite_Web_Server, SWT.BORDER);
			text_Web_Server_Port.setLayoutData(gridData_Text_Web_Server_Port);
		}
		
		return text_Web_Server_Port;
	}
	
	public Text get_Text_Proxy_Server()
	{
		if (text_Proxy_Server == null)
		{
			GridLayout gridLayout = new GridLayout();
			gridLayout.numColumns = 2;
			
			GridData gridData_Composite_Proxy_Server = new GridData();
			gridData_Composite_Proxy_Server.grabExcessHorizontalSpace = true;
			gridData_Composite_Proxy_Server.horizontalAlignment = SWT.FILL;
			
			composite_Proxy_Server = new Composite(group_Servers, SWT.NONE);
			composite_Proxy_Server.setLayout(gridLayout);
			composite_Proxy_Server.setLayoutData(gridData_Composite_Proxy_Server);
			
			label_Proxy_Server = new Label(composite_Proxy_Server, SWT.NONE);
			label_Proxy_Server.setText("Proxy:");
			
			GridData gridData_Text_Proxy_Server = new GridData();
			gridData_Text_Proxy_Server.grabExcessHorizontalSpace = true;
			gridData_Text_Proxy_Server.horizontalAlignment = SWT.FILL;
			text_Proxy_Server = new Text(composite_Proxy_Server, SWT.BORDER);
			text_Proxy_Server.setLayoutData(gridData_Text_Proxy_Server);
		}
		
		return text_Proxy_Server;
	}
	
	
	private Composite get_Composite_Buttons()
	{
		if (composite_Buttons == null)
		{
			GridLayout gridLayout_Composite_Buttons = new GridLayout();
			gridLayout_Composite_Buttons.numColumns = 2;
			
			GridData gridData_Composite_Buttons = new GridData();
			gridData_Composite_Buttons.grabExcessHorizontalSpace = true;
			gridData_Composite_Buttons.horizontalAlignment = SWT.FILL;
			
			composite_Buttons = new Composite(this, SWT.NONE);
			composite_Buttons.setLayout(gridLayout_Composite_Buttons);
			composite_Buttons.setLayoutData(gridData_Composite_Buttons);
			
			/**
			 * Save button
			 */
			GridData gridData_Button_Save = new GridData();
			gridData_Button_Save.grabExcessHorizontalSpace = true;
			gridData_Button_Save.horizontalAlignment = SWT.CENTER;
			
			button_Save = new Button(composite_Buttons, SWT.PUSH);
			button_Save.setLayoutData(gridData_Button_Save);
			button_Save.setText("Guardar");
			
			final Composite_Configuration compositeConfiguration = this;
			button_Save.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					
					/**
					 * Save inputs to configuration object
					 */
					fill_Configuration();
					
					Composite_Configuration_Event event = new Composite_Configuration_Event(compositeConfiguration);
					event.setEventType(Composite_Configuration_Event.Event_Types.SAVE);
					fire_Composite_Configuration_Event(event);
				}
			});
		
			
			/**
			 * Cancel button
			 */
			GridData gridData_Button_Cancel = new GridData();
			gridData_Button_Cancel.grabExcessHorizontalSpace = true;
			gridData_Button_Cancel.horizontalAlignment = SWT.CENTER;

			button_Cancel = new Button(composite_Buttons, SWT.PUSH);
			button_Cancel.setLayoutData(gridData_Button_Cancel);
			button_Cancel.setText("Cancelar");
			
			button_Cancel.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					Composite_Configuration_Event event = new Composite_Configuration_Event(compositeConfiguration);
					event.setEventType(Composite_Configuration_Event.Event_Types.CANCEL);
					fire_Composite_Configuration_Event(event);
				}
			});
			
			
		}
		
		return composite_Buttons;
	}
	
	
	
	
	
}
